from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon
from pygame import mixer
import sys, time
from threading import Thread
from form import Ui_MainWindow


class Air:
    # 空调开关常量
    switch = 0
    # 空调温度
    temperature = 0

    # 初始化
    def __init__(self):
        app = QtWidgets.QApplication(sys.argv)
        MainWindow = QtWidgets.QMainWindow()
        app.setWindowIcon(QIcon('./rescourse/logo.ico'))

        self.ui = Ui_MainWindow()

        self.ui.setupUi(MainWindow)
        self.ui.pushButton_3.clicked.connect(lambda: self.start_thread(self.air_switch))
        self.ui.pushButton.clicked.connect(lambda: self.start_thread(self.drop))
        self.ui.pushButton_4.clicked.connect(lambda: self.start_thread(self.drop))
        self.ui.pushButton_5.clicked.connect(lambda: self.start_thread(self.add))
        self.ui.pushButton_6.clicked.connect(lambda: self.start_thread(self.reduce))
        MainWindow.show()
        sys.exit(app.exec_())

    def start_thread(self, func):
        thread = Thread(target=func)
        thread.start()

    def add(self):
        # 判断空调是否打开
        if not self.switch < 1:
            current = self.ui.label_2.text().replace('℃', '')
            self.temperature = int(current)
            # 如果温度在1-30℃则可以增温
            if (self.temperature >= 1) and (self.temperature <= 29):
                self.temperature += 1
                self.ui.label_2.setText(f'{self.temperature}℃')
                self.drop()

    def reduce(self):
        # 判断空调是否打开
        if not self.switch < 1:
            current = self.ui.label_2.text().replace('℃', '')
            self.temperature = int(current)
            # 如果温度在1-30内则可以减温
            if (self.temperature >= 1) and (self.temperature <= 30):
                self.temperature -= 1
                self.ui.label_2.setText(f'{self.temperature}℃')
                self.drop()

    def drop(self):
        mixer.init()
        mixer.music.load('rescourse/audio/di.mp3')
        mixer.music.play()
        time.sleep(1)
        mixer.init()
        mixer.music.load('rescourse/audio/air-extractor-fan.mp3')
        mixer.music.play(loops=-1)

    def air_switch(self):
        self.switch += 1
        if not self.switch > 1:
            self.ui.label_2.setText('25℃')
            self.ui.pushButton_3.setStyleSheet("QPushButton{background-color: rgb(229, 57, 53);}\n"
                                               "QPushButton{color: rgb(255,255,255);  }\n"
                                               "QPushButton{border-radius: 30px;  }\n"
                                               "QPushButton:pressed{background-color: red;}\n"
                                               "QPushButton{border-image: url(rescourse/on.png);}\n"
                                               "QPushButton{border-style: outset;}")
            mixer.init()
            mixer.music.load('rescourse/audio/di.mp3')
            mixer.music.play()

            mixer.init()
            mixer.music.queue('rescourse/audio/ac-work.mp3')

            time.sleep(9)
            mixer.music.queue('rescourse/audio/air-extractor-fan.mp3')

        else:
            self.ui.label_2.setText('0℃')
            self.ui.pushButton_3.setStyleSheet("QPushButton{background-color: rgb(67, 160, 71);}\n"
                                               "QPushButton{color: rgb(255,255,255);  }\n"
                                               "QPushButton{border-radius: 30px;  }\n"
                                               "QPushButton:pressed{background-color: red;}\n"
                                               "QPushButton{border-image: url(rescourse/on.png);}\n"
                                               "QPushButton{border-style: outset;}")
            # 释放播放的资源
            mixer.music.unload()
            self.switch = 0


if __name__ == "__main__":
    app = Air()
